using System;
using System.Collections;
using Scenario.Modules.DM.DataModel;

namespace Scenario.Modules.DM.Application
{
	public class DocumentManagerApplication
	{

		public DocumentManagerHelper dmHelper;
		
		public DocumentManagerApplication()
		{
		}

		virtual public void InstanceHelper()
		{
			dmHelper = new DocumentManagerHelper();
		}

		public string CreateDocument (String sFunction)
		{
			// Initializes the out par
			Hashtable FormalParameter;
			Hashtable FormalParameterOut;
			Scenario.WorkflowEngine.DataModel.WorkflowApplicationsData.XMLApplicationFunction oXMLApplicationFunction;
			string sIdDocument="";
			string sDocumentVersion="";
			try
			{	
				Scenario.Applications.ApplicationInterface.ExploreSignature(sFunction,out oXMLApplicationFunction,out FormalParameter,out FormalParameterOut);

				string sIdMaster=(string) FormalParameter["IdMaster"];
				string sDocumentName=(string) FormalParameter["DocumentName"];

				dmHelper.CreateDocument(sIdMaster, sDocumentName, out sIdDocument, out sDocumentVersion);
				
				FormalParameterOut["IdDocument"]=sIdDocument;
				FormalParameterOut["DocumentVersion"]=sDocumentVersion;

				return Scenario.Applications.ApplicationInterface.ComposeResponse(oXMLApplicationFunction,0,"Zero",FormalParameterOut);
			}
			catch(System.Exception e)
			{
				//eccezione non gestita dal processo
				FormalParameterOut=new Hashtable();
				FormalParameterOut["ModuleFromType"]="KO";
				FormalParameterOut["Transfer"]="KO";
				oXMLApplicationFunction=new Scenario.WorkflowEngine.DataModel.WorkflowApplicationsData.XMLApplicationFunction();
                return Scenario.Applications.ApplicationInterface.ComposeResponse(oXMLApplicationFunction, 100, "CreateDocument.ERROR:" + e.Message, FormalParameterOut);
			}

		}

		
		public string AttachFile(String sFunction)
		{
			// Initializes the out par
			Hashtable FormalParameter;
			Hashtable FormalParameterOut;
            Scenario.WorkflowEngine.DataModel.WorkflowApplicationsData.XMLApplicationFunction oXMLApplicationFunction;

			try
			{
                Scenario.Applications.ApplicationInterface.ExploreSignature(sFunction, out oXMLApplicationFunction, out FormalParameter, out FormalParameterOut);

				string sIdMaster=(string) FormalParameter["IdMaster"];
				string sDocumentName=(string) FormalParameter["DocumentName"];
				string sFilePath=(string) FormalParameter["FilePath"];
				string sFileName=(string) FormalParameter["FileName"];
				string sFileExtension=(string) FormalParameter["FileExtension"];
				string sMigrable =(string) FormalParameter["Migrable"];

				XMLDocumentFile xmlFile = new XMLDocumentFile();
				xmlFile.Type = sFileExtension;
				xmlFile.Name = sFileName;
				xmlFile.Id = sFileName;
				xmlFile.MigratePolicy = (sMigrable == "YES") ? XMLDocumentFile.DOCUMENT_MIGRATE_POLICY : XMLDocumentFile.VERSION_MIGRATE_POLICY;

				dmHelper.AddAttach (sIdMaster, sDocumentName, xmlFile, sFilePath + sFileName + "." + sFileExtension);

                return Scenario.Applications.ApplicationInterface.ComposeResponse(oXMLApplicationFunction, 0, "Zero", FormalParameterOut);
			}
			catch(System.Exception e)
			{
				//eccezione non gestita dal processo
				FormalParameterOut=new Hashtable();
				FormalParameterOut["ModuleFromType"]="KO";
				FormalParameterOut["Transfer"]="KO";
				oXMLApplicationFunction=new Scenario.WorkflowEngine.DataModel.WorkflowApplicationsData.XMLApplicationFunction();
				return Scenario.Applications.ApplicationInterface.ComposeResponse(oXMLApplicationFunction,100,"AttachFile.ERROR:" + e.Message,FormalParameterOut);
			}
		}

		
	}
}
